package org.gl.training.planner.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.jms.ConnectionFactory;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import org.gl.training.planner.dao.UserDao;
import org.gl.training.planner.model.Constants;
import org.gl.training.planner.model.MailMessage;
import org.gl.training.planner.model.User;
import org.gl.training.planner.model.UserHistory;

@ManagedBean(name = "adminBean")
@RequestScoped
public class AdminBean {

	@Resource(mappedName = "jms/EmailConnectionFactory")
	private ConnectionFactory connectionFactory;

	@Resource(mappedName = "jms/EmailQueue")
	private Queue emailQueue;

	private List<User> userList;

	@EJB
	private UserDao userDao;

	private String infoMessage;

	private String errorMessage;

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getInfoMessage() {
		return infoMessage;
	}

	public void setInfoMessage(String infoMessage) {
		this.infoMessage = infoMessage;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public List<User> getUserList() {
		if (userList == null) {
			userList = userDao.getUsers();
		}
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public String approve() {
		List<User> userSelectedList = getSelectedUserList();

		for (User user : userSelectedList) {
			if (!user.getUserStatus().equals(Constants.USER_STATUS_APPROVED)) {
				user.setUserStatus(Constants.USER_STATUS_APPROVED);

				List<UserHistory> userHistoryList = user.getUserHistoryList();
				UserHistory userHistory = new UserHistory();
				userHistory.setTime(new Date());
				userHistory.setAction("User was approved");
				userHistory.setUser(user);
				user.setUserHistoryList(userHistoryList);
				userHistoryList.add(userHistory);

				userDao.save(user);
			}
		}

		return null;
	}

	public String delete() {
		List<User> userSelectedList = getSelectedUserList();

		for (User user : userSelectedList) {
			userDao.delete(user);
			userList.remove(user);
		}

		return null;
	}

	public String sendEmail() {
		List<User> userSelectedList = getSelectedUserList();

		for (User user : userSelectedList) {
			MailMessage message = new MailMessage();
			message.setSubject("Confirmation");
			message.setBody("Congratulations, you are registered");
			message.setTo(user.getEmail());

			sendEmailToJms(message);
		}
		return null;
	}

	private void sendEmailToJms(MailMessage mailMessage) {
		try {
			QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) connectionFactory;
			QueueConnection queueConnection = queueConnectionFactory
					.createQueueConnection();
			QueueSession session = queueConnection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			QueueSender sender = session.createSender(emailQueue);
			ObjectMessage msg = session.createObjectMessage(mailMessage);
			sender.send(msg);
			infoMessage = "E-mail sent successfully";
		} catch (Exception e) {
			errorMessage = "Emails not sent. " + e.getMessage();
		}
	}

	private List<User> getSelectedUserList() {
		List<User> userSelectedList = new ArrayList<User>();
		for (User user : userList) {
			if (user.isSelected()) {
				userSelectedList.add(user);
			}
		}
		return userSelectedList;
	}

}
